package cn.wzy.onlyoffice.utils.cos;

import cn.wzy.onlyoffice.config.CosConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.Bucket;
import com.qcloud.cos.model.CannedAccessControlList;
import com.qcloud.cos.model.CreateBucketRequest;

import java.util.List;


/**
 * 使用cos对数据桶的基本操作
 */
public class cosBucketUtil {

    /***
     * 创建桶
     * cosClient 客户端连接对象
     * bucketName 自定义名称
     *
     * */
    public static Bucket createBucket(COSClient cosClient, String bucketName) throws CosClientException, CosServiceException {
        String bucket = bucketName + "-" + CosConfig.Tencent_appid; //存储桶名称，格式：BucketName-APPID
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
// 设置 bucket 的权限为 Private(私有读写), 其他可选有公有读私有写, 公有读写
        createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        Bucket bucketResult = cosClient.createBucket(createBucketRequest);
        return bucketResult;
    }

    ;

    /**
     * 查询桶列表
     * cosClient 客户端连接对象
     */
    public static List<Bucket> listBuckets(COSClient cosClient) throws CosClientException, CosServiceException {
        // 如果只调用 listBuckets 方法，则创建 cosClient 时指定 region 为 new Region("") 即可
        List<Bucket> buckets = cosClient.listBuckets();
        return buckets;
    }

    /**
     * 删除存储桶
     * 功能说明
     * 删除指定账号下的空存储桶。
     * cosClient 客户端连接对象，桶名称
     **/
    public static void deleteBucket(COSClient cosClient, String bucketName) throws CosClientException, CosServiceException {
        cosClient.deleteBucket(bucketName);
    }

    /**
     * 判断某个桶是否存在
     * cosClient 初始化桶
     * bucketName 桶名称
     */
    public static Boolean getdoesBucketExist(COSClient cosClient, String bucketName) {
        return cosClient.doesBucketExist(bucketName + "-" + CosConfig.Tencent_appid);
    }

    public static void main(String[] args) {
     /*   //1.获取桶连接
        final COSClient cosClient = cosBucketUtil.getCosClient();
        String buckentName="ee";
        //2.判断桶是否存在
        final Boolean aBoolean = cosBucketUtil.getdoesBucketExist(cosClient, buckentName);
        if (aBoolean){
            //查询桶列表
             List<Bucket> buckets = cosBucketUtil.listBuckets(cosClient);
            for (Bucket bucket : buckets) {
                System.out.println("桶名称"+bucket.getName());
                System.out.println("桶的地域"+bucket.getLocation());
            }
        }else {
            //新增桶
             Bucket bucket = cosBucketUtil.createBucket(cosClient, buckentName);
        }*/
        COSClient cosClient = CosConfig.getCosClient();
        Boolean aBoolean = cosBucketUtil.getdoesBucketExist(cosClient, "onlyoffice-1322421196");
        System.out.println(aBoolean);
        //删除桶
        //cosBucketUtil.deleteBucket(cosClient,"ee-1311510668");
    }
}